var express = require('express');
var router = express.Router();
var session = require("express-session");
router.use(session({
    secret:"chenbeibei",
    resave:false,
    saveUninitialized:true
}));
var Scenery = require("../bin/scenery-dao.js");
var Intro = require("../bin/intro-dao.js");


// 根据精彩推荐或超低折扣查询渲染首页的数据
// 将来如果数据多的话，这里要改按多少条进行查询，不能一下子全查出来了。
router.post("/show",(req,res)=>{
  Scenery.find({show:req.body.show})
  .limit(req.body.page*5)
  .then(data=>{
    res.json({err:0,msg:data});
  });
})
router.post("/showtwo",(req,res)=>{
  Scenery.find({show:req.body.show})
  .limit(req.body.page*5)
  .then(data=>{
    res.json({err:0,msg:data});
  });
})

// 根据type类型（亲子游）或theme类型（主题）查询渲染景点列表页的数据
router.post("/title",(req,res)=>{
  var obj = {};
   if(req.body.bottom && !req.body.top){
      obj={price:{$gte:req.body.bottom}};
  }else if(!req.body.bottom && req.body.top){
      obj={price:{$lte:req.body.top}};
  }else if(req.body.bottom && req.body.top){
      obj={price:{$lte:req.body.top,$gte:req.body.bottom}};
  }else{
    obj = {price:{$gte:0}};
  }

  if(
    req.body.title=="亲子游"||
    req.body.title=="情侣游"||
    req.body.title=="朋友游"||
    req.body.title=="家庭游"||
    req.body.title=="团体游"
    ){
      if(req.body.grade){
        var conObj = {type:req.body.title,porder:obj.price,level:req.body.grade};
      }else{
        var conObj = {type:req.body.title,porder:obj.price};
      }
  }else{
    if(req.body.grade){
      var conObj = {theme:req.body.title,porder:obj.price,level:req.body.grade};
    }else{
      var conObj = {theme:req.body.title,porder:obj.price};
    }
  };
  if(req.body.order){
    Scenery.find(conObj)
    .sort({porder:req.body.order})
    .limit(req.body.page*5)
    .then(data=>{
      res.json({err:0,msg:data});
    });
  }else{
    Scenery.find(conObj)
    .limit(req.body.page*5)
    .then(data=>{
      res.json({err:0,msg:data});
    });
  }
});

// 根据传过来的景点的_id去查这个景点的详细信息，用于渲染景点详情页
router.post("/detail",(req,res)=>{
  Scenery.findOne(req.body)
  .then(data=>{
    res.json({err:0,msg:data});
  });
})

// 根据传过来的景点名字查该景点的详细介绍
router.post("/intro",(req,res)=>{
  Intro.findOne(req.body)
  .then(data=>{
    res.json({err:0,msg:data});
  });
})

// 根据搜索项（城市名或主题名或景点名）搜索
router.post("/search",(req,res)=>{
  var reg = new RegExp(req.body.txt);
  if(
    req.body.txt=="自然风景"||
    req.body.txt=="岛屿潜水"||
    req.body.txt=="温泉水疗"||
    req.body.txt=="主题乐园"||
    req.body.txt=="人文建筑"||
    req.body.txt=="名胜古迹"||
    req.body.txt=="漂流探险"||
    req.body.txt=="展览演出"||
    req.body.txt=="滑雪冰雕"||
    req.body.txt=="水上娱乐"||
    req.body.txt=="民族宗教"||
    req.body.txt=="海滨风景"
  ){
    Scenery.find({theme:req.body.txt})
    .then(data=>{
      res.json({err:0,msg:data});
    });
  }else{
    Scenery.find({name:reg})
    .then(data=>{
      res.json({err:0,msg:data});
    });
  }
});

// 根据viewListTwo.vue中的条件查询进行搜索
router.post("/condition",(req,res)=>{
   var obj = {};
   if(req.body.bottom && !req.body.top){
      obj={price:{$gte:req.body.bottom}};
  }else if(!req.body.bottom && req.body.top){
      obj={price:{$lte:req.body.top}};
  }else if(req.body.bottom && req.body.top){
      obj={price:{$lte:req.body.top,$gte:req.body.bottom}};
  }else{
    obj = {price:{$gte:0}};
  }
  var reg = new RegExp(req.body.txt);
  if(
    req.body.txt=="自然风景"||
    req.body.txt=="岛屿潜水"||
    req.body.txt=="温泉水疗"||
    req.body.txt=="主题乐园"||
    req.body.txt=="人文建筑"||
    req.body.txt=="名胜古迹"||
    req.body.txt=="漂流探险"||
    req.body.txt=="展览演出"||
    req.body.txt=="滑雪冰雕"||
    req.body.txt=="水上娱乐"||
    req.body.txt=="民族宗教"||
    req.body.txt=="海滨风景"
  ){
    if(req.body.grade){
      var conObj = {theme:req.body.txt,porder:obj.price,level:req.body.grade};
    }else{
      var conObj = {theme:req.body.txt,porder:obj.price};
    }
    if(req.body.order){
      Scenery.find(conObj)
      .sort({porder:req.body.order})
      .limit(req.body.page*5)
      .then(data=>{
        res.json({err:0,msg:data});
      });
    }else{
      Scenery.find(conObj)
      .limit(req.body.page*5)
      .then(data=>{
        res.json({err:0,msg:data});
      });
    }
  }else{
    if(req.body.grade){
      var conObj = {name:reg,porder:obj.price,level:req.body.grade};
    }else{
      var conObj = {name:reg,porder:obj.price};
    }
    if(req.body.order){
      Scenery.find(conObj)
      .sort({porder:req.body.order})
      .limit(req.body.page*5)
      .then(data=>{
        res.json({err:0,msg:data});
      });
    }else{
      Scenery.find(conObj)
      .limit(req.body.page*5)
      .then(data=>{
        res.json({err:0,msg:data});
      });
    }
  }


});



module.exports = router;
